package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.UcenterMember;
import com.atguigu.serviceedu.entity.vo.MemberQuery;
import com.atguigu.serviceedu.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author gengky
 * @since 2022-04-15
 */
@RestController
@RequestMapping("/eduService/member")
public class UcenterMemberController {


    @Autowired
    private UcenterMemberService ucenterMemberService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "所有用户列表")
    @GetMapping("findAll")
    public R findAllMember(){
        List<UcenterMember> list = ucenterMemberService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 根据ID删除（逻辑删除）
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除用户")
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id){
        boolean flag=ucenterMemberService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 动态条件查询，+，分页查询用户列表
     * @param current
     * @param limit
     * @param memberQuery
     * @return
     */
    @ApiOperation(value = "动态条件查询，分页用户列表")
    @PostMapping("PageCondition/{current}/{limit}")
    public R pageMemberCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) MemberQuery memberQuery){

        System.out.println("---------------------");
        System.out.println("动态条件查询，分页用户列表");

        //创建page对象
        Page<UcenterMember> pageMember=new Page<>(current,limit);
        //构建条件
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();

        //多条件组合查询（动态sql组合）
        if (!StringUtils.isEmpty(memberQuery.getNickname())){
            //构造条件（这里“name”是mysql中的列名，后面一样）
            wrapper.like("nickname",memberQuery.getNickname());
        }
        if (!StringUtils.isEmpty(memberQuery.getMobile())){
            wrapper.eq("mobile",memberQuery.getMobile());
        }
        if (!StringUtils.isEmpty(memberQuery.getSex())){
            wrapper.eq("sex",memberQuery.getSex());
        }
        if (!StringUtils.isEmpty(memberQuery.getBegin())){
            wrapper.ge("gmt_create",memberQuery.getBegin());
        }
        if (!StringUtils.isEmpty(memberQuery.getEnd())){
            wrapper.le("gmt_create", memberQuery.getEnd());
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        ucenterMemberService.page(pageMember,wrapper);
        //
        List<UcenterMember> records = pageMember.getRecords();
        //
        long total=pageMember.getTotal();

        //
        System.out.println(records.size());

        return R.ok().data("total",total).data("rows",records);

    }


    /**
     * 根据用户名查询
     * @param username
     * @return
     */
    @GetMapping("selectByName/{username}")
    public R SelectByName(@PathVariable String username){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", username);
        List<UcenterMember> ucenterMembers= (List<UcenterMember>) ucenterMemberService.listByMap(map);
        ucenterMembers.forEach(System.out::println);
        return R.ok().data("data",ucenterMembers);
    }


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询")
    @GetMapping("selectById/{id}")
    public R getById(@PathVariable String id){
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        return R.ok().data("member", ucenterMember);
    }

    /**
     * 根据ID修改
     * @param ucenterMember
     * @return
     */
    @ApiOperation(value = "根据ID修改")
    @PostMapping("updateMemberById/{id}")
    public R updateById(@RequestBody UcenterMember ucenterMember){
        boolean flag = ucenterMemberService.updateById(ucenterMember);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


    /**
     * 添加或修改
     * @param ucenterMember
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PostMapping("saveOrUpdateMember")
    public R saveOrUpdateMember(@RequestBody UcenterMember ucenterMember){
        boolean flag = ucenterMemberService.saveOrUpdate(ucenterMember);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }



    /**
     * 新增
     * @param ucenterMember
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PostMapping("addMember")
    public R addMember(@RequestBody UcenterMember ucenterMember){
        boolean save = ucenterMemberService.save(ucenterMember);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

